package com.billdowney.controller.system.quartz;

import org.quartz.JobKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.quartz.QuartzProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.billdowney.annotation.AuthRoleAllowed;
import com.billdowney.controller.BaseController;
import com.billdowney.service.interfaces.system.quartz.QuartzManageService;
import com.billdowney.util.auth.AuthRoleConst;
import com.billdowney.util.quartz.QuartzConst;
import com.billdowney.util.return_type.ReturnData;
import com.billdowney.vo.system.quartz.JobDetailInfoVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 定时任务控制器
 * 
 * @author 超级小富翁 - BillDowney
 * @date 2019年10月14日 上午11:35:47
 */
@RestController
@RequestMapping(value = "/quartz", method = { RequestMethod.POST })
@Api(tags = "定时任务控制器")
public class QuartzManageController extends BaseController {

	@Autowired
	private QuartzManageService quartzManageService;

	@AuthRoleAllowed(AuthRoleConst.ROLE_SUPERADMIN)
	@RequestMapping(value = "/getConfig")
	@ApiOperation(value = "获取quartz定时任务配置", response = QuartzProperties.class)
	public Object getConfig() throws Exception {
		return new ReturnData(quartzManageService.getQuartzProperties());
	}

	@AuthRoleAllowed(AuthRoleConst.ROLE_SUPERADMIN)
	@RequestMapping(value = "/getAllJobClass")
	@ApiOperation(value = "获取所有实现Job的类", response = JobDetailInfoVO.class)
	public Object getAllJobClass() throws Exception {
		return new ReturnData(quartzManageService.getAllJobClass());
	}

	@AuthRoleAllowed(AuthRoleConst.ROLE_SUPERADMIN)
	@RequestMapping(value = "/getJobDetailInfo")
	@ApiOperation(value = "根据JobKey获取指定Job的详情", response = JobDetailInfoVO.class)
	@ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "键名", required = true),
			@ApiImplicitParam(name = "group", value = "组名", defaultValue = QuartzConst.JOB_GROUP_NAME) })
	public Object getJobDetailInfo(@RequestParam(required = true) String name,
			@RequestParam(defaultValue = QuartzConst.JOB_GROUP_NAME) String group) {
		return new ReturnData(quartzManageService.getJobDetailInfo(new JobKey(name, group)));
	}

	@AuthRoleAllowed(AuthRoleConst.ROLE_SUPERADMIN)
	@RequestMapping(value = "/getSchedulerJobDetailList")
	@ApiOperation(value = "获取调度器中所有的作业列表", response = JobDetailInfoVO.class)
	public Object getSchedulerJobDetailList() {
		return new ReturnData(quartzManageService.getSchedulerJobDetailList());
	}

	@AuthRoleAllowed(AuthRoleConst.ROLE_SUPERADMIN)
	@RequestMapping(value = "/getJobTriggerList")
	@ApiOperation(value = "获取所有作业的触发器", response = JobDetailInfoVO.class)
	@ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "键名", required = true),
			@ApiImplicitParam(name = "group", value = "组名", defaultValue = QuartzConst.JOB_GROUP_NAME) })
	public Object getJobTriggerList(@RequestParam(required = true) String name,
			@RequestParam(defaultValue = QuartzConst.JOB_GROUP_NAME) String group) {
		return new ReturnData(quartzManageService.getJobTriggerList(new JobKey(name, group)));
	}

	@AuthRoleAllowed(AuthRoleConst.ROLE_SUPERADMIN)
	@RequestMapping(value = "/addJob")
	@ApiOperation(value = "持久化一个作业到调度器中", notes = "具有相同名称的作业已经存在，并且replace为false会抛出异常", response = JobDetailInfoVO.class)
	@ApiImplicitParams({ @ApiImplicitParam(name = "infoVO", value = "需要添加的作业信息"),
			@ApiImplicitParam(name = "replace", value = "是否替换", defaultValue = "true", dataTypeClass = Boolean.class) })
	public Object addJob(JobDetailInfoVO infoVO, @RequestParam(defaultValue = "true") boolean replace) {
		return new ReturnData(quartzManageService.addJob(infoVO, replace));
	}

	@AuthRoleAllowed(AuthRoleConst.ROLE_SUPERADMIN)
	@RequestMapping(value = "/xxxx")
	@ApiOperation(value = "获取目录列表", notes = "", response = JobDetailInfoVO.class)
	@ApiImplicitParams({ @ApiImplicitParam(name = "parentId", value = "父级id，为空的时候则查询全部数据"),
			@ApiImplicitParam(name = "type", value = "目录类别", required = true) })
	public Object xxxx() {
		return new ReturnData();
	}

}
